package com.fingard.dsp.bank.directbank.nccb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class NCCB01Balance extends NCCB01Base implements IBalance {


    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            //发银行报文的XML对象
            XmlTextWriter xmlWriter = new XmlTextWriter();
            NCCB01Header header = new NCCB01Header();
            header.transCode = "eacqry.AcBalQry.do";
            header.serialNo = p_req7506.reqHeader.transSeqID;
            header.msgType = "0";
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("Body");
            while (p_req7506.readRecord()) {
                xmlWriter.writeElementString("AcNo", p_req7506.readedDetail.actNum);
            }
            xmlWriter.writeEndDocument();
            // 发送银行
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),header);
            if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        //判断头部状态码

                        //获取银行响应代码，返回值为000或002表示处理成功，否则为处理失败
                        String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/RejCode");
                        String respondInfo = tmpBkReader.getFirstText("/Message/Head/RejMsg");
                        if(tmpRetCode.equals("FIN0000")) {
                            String acNo = tmpBkReader.getFirstText("/Message/Body/AcNo");
                            while (tmpBkReader.read("/Message/Body/ActiveList/Record")) {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.actNum = acNo;
                                String curType = tmpBkReader.readedXElement.getSingleText("Currency");
                                tmpRetDetail.actCur = getBankConfig().getMapKeyByValue("curActMapping", curType);
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            }
                        }else{
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(respondInfo);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }

}
